package com.llap.exp190929.controller;

import com.llap.exp190929.dto.PaginatedDto;
import com.llap.exp190929.entity.Product;
import com.llap.exp190929.service.ProductService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * Created by LLAP on 2019/10/14.
 * Copyright (c) 2019 LLAP. All rights reserved.
 */
@RestController
@RequestMapping("products")
@AllArgsConstructor
@CrossOrigin
@Slf4j
public class ProductsController {
    private ProductService productService;

    @GetMapping("")
    public PaginatedDto<Product> findProducts(
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "20") Integer pageSize,
            @RequestParam(required = false, defaultValue = "") String keyword) {
        return PaginatedDto.from(productService.findProducts(page, pageSize, keyword));
    }

    @GetMapping("{id}")
    public Product findProduct(@PathVariable Integer id) {
        return productService.findProduct(id);
    }

    @PostMapping("")
    public Product addProduct(@RequestBody Product product) {
        return productService.addProduct(product);
    }

    @DeleteMapping("")
    public void deleteProduct(@RequestParam Integer id) {
        productService.deleteProduct(id);
    }

    @PatchMapping("")
    public Product updateProduct(@RequestBody Product product) {
        return productService.updateProduct(product);
    }
}
